package com.linwen.javase.thread;

/**同时往一个银行账户存钱
 * @author : linwen
 * @date : 上午1:15 2021/1/12
 */
public class 线程锁练习1 {
    public static void main(String[] args) {
        Account account = new Account(0);
        Custerm custerm1 = new Custerm(account);
        Custerm custerm2 = new Custerm(account);
        Thread thread1 = new Thread(custerm1);
        Thread thread2 = new Thread(custerm2);
        thread1.setName("111");
        thread2.setName("222");
        thread1.start();
        thread2.start();
    }
}
class Account{
    private double banlace = 0;

    public Account(double banlace) {
        this.banlace = banlace;
    }
    public synchronized void  save(double saveMoney){
        if(saveMoney>0){
            banlace += saveMoney;
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"success"+banlace);
        }else{
            System.out.println("err saveMoney is error");
        }
    }
}
class Custerm implements Runnable{
    private Account account;

    public Custerm(Account account) {
        this.account = account;
    }

    @Override
    public void run() {
        for (int i = 0; i < 3; i++) {
            account.save(100);
        }
    }
}
